package unit10.paramterizeMethod;

/**
 * @author v_irlwang
 * @date 2021/5/11
 */
public class Example {

    private int getValue() {
        return 1;
    }

    private void test1(){

    }

    /**
     * before refactoring
     */
    private double baseCharge() {
        double result = Math.min(getValue(), 100) * 0.03;
        if (getValue() > 100) {
            result += (Math.min(getValue(), 200) - 100) * 0.05;
        }
        if (getValue() > 200) {
            result += (getValue() - 200) * 0.07;
        }
        return result;
    }

    /**
     * after refactoring
     */
    private double baseCharge2() {
        double result = usageInRange(0, 100) * 0.03;
        result += usageInRange(100, 200) * 0.5;
        result += usageInRange(200, Integer.MAX_VALUE) * 0.07;
        return result;
    }

    private int usageInRange(int start, int end) {
        if (getValue() > start) {
            return Math.min(getValue(), end) - start;
        }
        return 0;
    }

}
